﻿/*-----------------------------------------------
// File: BattleFactory.cs 
// Description: 战场对象生成工场 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class BattleFactory
    {
        public static BattleCharacter CreateBattleCharacter() {
            GameObject obj = new GameObject();
            return obj.AddMissingComponent<BattleCharacter>();
        }

        //public static BattleProjectile CreateBattleProjectile(ProjectilePacket pp) {
        //    var particleObj = SpawnParticle(pp.projectileData);
        //    if (particleObj.obj != null)
        //    {
        //        BattleProjectile bp = null;
        //        switch (pp.projectileData.projFlyType)
        //        {
        //            case ProjectileFlyType.LINER:
        //                bp = particleObj.obj.AddMissingComponent<LinerProjectile>();
        //                break;
        //            case ProjectileFlyType.PALABORA:
        //                bp = particleObj.obj.AddMissingComponent<PalaboraProjectile>();
        //                break;
        //            default:
        //                bp = particleObj.obj.AddMissingComponent<BattleProjectile>();
        //                break;
        //        }
        //        bp.Init(pp);
        //        return bp;
        //    }
        //    return null;
        //}

        private static IEnumerator RegistPool(string abName, string assetName, LoadCompleteNotify callback) {
            AssetLoad.AssetBundleManager.Instance.LoadAssetBundle(abName);
            while (!AssetLoad.AssetBundleManager.Instance.IsLoadedAssetBundle(abName)) {
                yield return null;
            }
            GameObjectPoolManager.Instance.Regist(abName, assetName);
            if (callback != null) {
                callback();
            }
            //Debug.Log("RegistPool. " + abName + " " + assetName);
        }

        private static void UnRegistPool(string abName, string assetName) {
            GameObjectPoolManager.Instance.UnRegist(abName, assetName);
            AssetLoad.AssetBundleManager.Instance.UnloadAssetBundle(abName);
        }


        private static void RegistPool(string path) {
            AssetLoad.ResourceManager.Instance.LoadResource(path);
            GameObjectPoolManager.Instance.Regist(path);
        }
        private static void UnRegistPool(string path) {
            GameObjectPoolManager.Instance.UnRegist(path);
            AssetLoad.ResourceManager.Instance.UnloadResource(path);
        }

        #region Common Pool
        public const string wordTipPrefabPath = @"UIcomponent/WordTip";
        public const string bloodBarPrefabPath = @"UIcomponent/BloodBar";
        //public const string dropBallPrefabPath = @"particles/UI_DXP/UI_baodou01";
        public const string dropBallId = "4";
        public const string dropBallDisapperaId = "18";
        public const string dropFireBallId = "6";
        public const string dropFireBallDisappearId = "17";
        public const string buffIconPrefab = @"UIcomponent/BuffIcon";

        private static ParticleData dropBallData;
        private static ParticleData dropBallDisappearData;
        private static ParticleData fireBallData;
        private static ParticleData fireBallDisappearData;

        private static ParticleData deadData;
        private static ParticleData rushData;
        private static ParticleData singData;

        private static string GetAssetName(string abName) {
            return CommonMethod.GetAssetNameFromAssetBundleName(abName);
        }

        public static IEnumerator RegistCommon() {
            RegistPool(wordTipPrefabPath);
            RegistPool(bloodBarPrefabPath);
            RegistPool(buffIconPrefab);

            dropBallData = ParticleData.CreateCommonParticleData(dropBallId);
            dropBallDisappearData = ParticleData.CreateCommonParticleData(dropBallDisapperaId);
            fireBallData = ParticleData.CreateCommonParticleData(dropFireBallId);
            fireBallDisappearData = ParticleData.CreateCommonParticleData(dropFireBallDisappearId);
            deadData = ParticleData.CreateCommonParticleData(Dict.Blo.DictConfigBlo.GetDeadParticleId());
            rushData = ParticleData.CreateCommonParticleData(Dict.Blo.DictConfigBlo.GetRushParticleId());
            singData = ParticleData.CreateCommonParticleData(Dict.Blo.DictConfigBlo.GetUltiSingParticleId());

#if !FIX_RESOURCE

            int taskCount = 0;
            ParticleFactory.RegistParticle(dropBallData.resUrl, () => { taskCount--; });
            taskCount++;
            ParticleFactory.RegistParticle(dropBallDisappearData.resUrl, () => { taskCount--; });
            taskCount++;
            ParticleFactory.RegistParticle(fireBallData.resUrl, () => { taskCount--; });
            taskCount++;
            ParticleFactory.RegistParticle(fireBallDisappearData.resUrl, () => { taskCount--; });
            taskCount++;
            ParticleFactory.RegistParticle(deadData.resUrl, () => { taskCount--; });
            taskCount++;
            ParticleFactory.RegistParticle(rushData.resUrl, () => { taskCount--; });
            taskCount++;
            ParticleFactory.RegistParticle(singData.resUrl, () => { taskCount--; });
            taskCount++;

            while (taskCount > 0) {
                yield return null;
            }
#else
            RegistPool(dropBallPrefabPath);
            RegistPool(dropBallDisappearPrefabPath);
            RegistPool(dropFireBallPrefabPath);
            RegistPool(dropFireBallDisapperaPrefab);

            var deadParticleData = ParticleData.CreateParticleData(Dict.Blo.DictConfigBlo.GetDeadParticleId());
            var rushParticleData = ParticleData.CreateParticleData(Dict.Blo.DictConfigBlo.GetRushParticleId());
            var ultiSingParticleData = ParticleData.CreateParticleData(Dict.Blo.DictConfigBlo.GetUltiSingParticleId());
            if (deadParticleData != null)
            {
                RegistParticle(deadParticleData.resUrl);
            }
            if (rushParticleData != null)
            {
                RegistParticle(rushParticleData.resUrl);
            }
            if (ultiSingParticleData != null)
            {
                RegistParticle(ultiSingParticleData.resUrl);
            }
            yield return 0;
#endif

        }

        public static void UnRegistCommon()
        {
            UnRegistPool(wordTipPrefabPath);
            UnRegistPool(bloodBarPrefabPath);
            UnRegistPool(buffIconPrefab);

#if !FIX_RESOURCE
            ParticleFactory.UnRegistParticle(dropBallData.resUrl);
            ParticleFactory.UnRegistParticle(dropBallDisappearData.resUrl);

            ParticleFactory.UnRegistParticle(fireBallData.resUrl);
            ParticleFactory.UnRegistParticle(fireBallDisappearData.resUrl);
#else
            UnRegistPool(dropBallPrefabPath);
            UnRegistPool(dropBallDisappearPrefabPath);

            UnRegistPool(dropFireBallPrefabPath);
            UnRegistPool(dropFireBallDisapperaPrefab);
#endif

            ParticleFactory.UnRegistParticle(deadData.resUrl);
            ParticleFactory.UnRegistParticle(rushData.resUrl);
            ParticleFactory.UnRegistParticle(singData.resUrl);
        }
        
        public static GameObject SpawnWordTip() {
            return GameObjectPoolManager.Instance.Spawn(wordTipPrefabPath);
        }

        public static GameObject SpawnBloodBar()
        {
            return GameObjectPoolManager.Instance.Spawn(bloodBarPrefabPath);
        }

        public static GameObject SpawnDropBall() {
            GameObject obj = ParticleFactory.SpawnParticle(dropBallData.resUrl);
            obj.AddComponent<DropAngerBall>();
            return obj;
        }

        public static GameObject SpawnDropBallDispear() {
            return ParticleFactory.SpawnParticle(dropBallDisappearData.resUrl);
        }
        public static GameObject SpawnDropFireBall() {
            GameObject obj = ParticleFactory.SpawnParticle(fireBallData.resUrl);
            obj.AddComponent<DropFireBall>();
            return obj;
        }
        public static GameObject SpawnDropFireBallDispear() {
            return ParticleFactory.SpawnParticle(fireBallDisappearData.resUrl);
        }

        public static GameObject SpawnBuffIcon() {
            var obj = GameObjectPoolManager.Instance.Spawn(buffIconPrefab);
            return obj;
        }

        public static void UnSpawn(GameObject wordTip) {
            GameObjectPoolManager.Instance.Unspawn(wordTip);
        }
        public static void UnSpawn(GameObject wordTip, float delay) {
            GameObjectPoolManager.Instance.Unspawn(wordTip, delay);
        }
#endregion


        public static ParticleData GetDeadParticleData()
        {
            return deadData;
        }

        public static ParticleData GetRushParticleData()
        {
            return rushData;
        }

        public static ParticleData GetSingParticleData()
        {
            return singData;
        }

    }
}
